home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / src / popen.c < prev    next >
C/C++ Source or Header  |  1996-01-04  |  811b  |  51 lines

  1. #include "amiga.h"
  2. #include "files.h"
  3. #include "amigados.h"
  4. #include <fcntl.h>
  5. #include <string.h>
  6.  
  7. #undef ERROR
  8. #define ERROR do { _seterr(); return(NULL); } while(0)
  9.  
  10. FILE *popen(const char *path, const char *mode)
  11. {
  12.     int fd;
  13.     BPTR fh;
  14.     long fdflags, fhflags;
  15.     char *apipe = malloc(strlen(path)+7);
  16.  
  17.     __chkabort();
  18.  
  19.     if (!apipe) {
  20.     errno = ENOMEM;
  21.     return(NULL);
  22.     }
  23.  
  24.     strcpy(apipe, "APIPE:");
  25.     strcat(apipe, path);
  26.  
  27.     if (*mode == 'w') {
  28.     fdflags = FI_WRITE;
  29.     fhflags = MODE_NEWFILE;
  30.     } else {
  31.     fdflags = FI_READ;
  32.     fhflags = MODE_OLDFILE;
  33.     }
  34.  
  35.     if (!(fh = Open(apipe, fhflags)))
  36.     ERROR;
  37.  
  38.     fd = _alloc_amigafd(fh, -1, fdflags);
  39.     if (fd < 0) {
  40.     Close(fh);
  41.     return(NULL);
  42.     }
  43.     return(fdopen(fd, mode));
  44. }
  45.  
  46. int pclose(FILE *stream)
  47. {
  48.     return(fclose(stream));
  49. }
  50.  
  51.